package com.yjtravel.observer.eventbus;

import com.google.common.base.Preconditions;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author weiqi
 * @version 1.0
 * Create by 2024/7/11 8:55
 */
public class ObserverAction {

    /**
     * target 表示观察者类，method 表示方法
     */
    private Object target;

    private Method method;

    public ObserverAction(Object target, Method method) {
        this.target = Preconditions.checkNotNull(target);
        this.method = method;
        this.method.setAccessible(true);
    }

    /**
     * 执行方法
     * @param event 方法的参数
     */
    public void execute(Object event){
        try {
            method.invoke(target, event);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
